package indi.cloud.common.elasticsearch.impl;

import indi.cloud.common.elasticsearch.EsBondsService;
import indi.cloud.common.elasticsearch.EsDataService;
import indi.cloud.common.elasticsearch.core.EsResult;
import indi.cloud.common.elasticsearch.core.EsUtils;
import indi.cloud.common.model.esresult.EsBondsListEntity;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yangyifan
 * @version 1.0
 * @date 2024/3/8 17:36
 * @Description
 */
@Service
public class EsBondServiceImpl implements EsBondsService {
    @Resource
    private EsDataService esDataService;

    @Override
    public EsResult<List<EsBondsListEntity>> queryBonds(JSONObject param) {
        // 主查询体
        BoolQueryBuilder mainBuilder = QueryBuilders.boolQuery();
        //信用代码
        String creditCodes = param.getString("creditCodes");
        //债券类型 bondType
        String bondTypes = param.getString("debType");
        //发行方式 issuingMethod
        String issuingMethod = param.getString("issuingMethod");
        //是否担保
        String isGuarantee = param.getString("isGuarantee");
        //交易市场
        String market = param.getString("market");
        //状态（正常、展期、违约）
        String state = param.getString("state");
        //特殊条款 （回售、永续、提前偿还、不含权、其他+）
        String specificItem = param.getString("specificItem");

        //债项评级
        String newRate = param.getString("newRate");

        //剩余规模
        // 剩余规模开始值
        String remainingScaleBegin = param.getString("remainingScaleBegin");
        // 剩余规模结束值
        String remainingScaleEnd = param.getString("remainingScaleEnd");

        //最新票据
        // 最新票据开始值
        String couponRateBegin = param.getString("couponRateBegin");
        // 最新票据结束值
        String couponRateEnd = param.getString("couponRateEnd");

        //剩余期限
        String debTime = param.getString("debTime");

        //利差
        // 利差开始值
        String interestRateSpreadBegin = param.getString("interestRateSpreadBegin");
        // 利差结束值
        String interestRateSpreadEnd = param.getString("interestRateSpreadEnd");

        Query query = new EsUtils(mainBuilder)
                .in("issuer_code.keyword", creditCodes)
                .in("deb_type.keyword", bondTypes)
                .in("is_guarantee", isGuarantee)
                .in("market.keyword", market)
                .in("issuing_method", issuingMethod)
                .in("new_rate.keyword", newRate)
                .in("termtomaturity_tag.keyword", debTime)
                .range("remaining_sum", remainingScaleBegin, remainingScaleEnd)
                .range("coupon_rate", couponRateBegin, couponRateEnd)
                .range("interest_rate_spread", interestRateSpreadBegin, interestRateSpreadEnd)
                .build();
        EsResult<List<EsBondsListEntity>> search1 = esDataService.searchScroll(EsBondsListEntity.class, query, "es_deb_debenture_base");
        search1.getData().stream().forEach(x -> x.setMarketName(marketNameConvert(x.getMarket())));
        return search1;
    }

    public String marketNameConvert(String market) {
        if (StringUtils.isNotBlank(market)) {
            String marketName = null;
            switch (market) {
                case "001001":
                case "001004":
                case "001066":
                case "001071":
                    marketName = "其他";
                    break;
                case "001002":
                    marketName = "上海证券交易所";
                    break;
                case "001003":
                    marketName = "深圳证券交易所";
                    break;
                case "001005":
                    marketName = "银行间市场";
                    break;
                case "001006":
                    marketName = "上交所固定收益平台";
                    break;
                case "001007":
                    marketName = "债券柜台交易市场";
                    break;
                case "001018":
                    marketName = "深交所综台协议交易平台";
                    break;
            }
            return marketName;
        }
        return null;
    }
}
